const express = require('express')
const router = express.Router()

const FocusModel = require('../../model/focusModel')
const { getUnix, multer } = require('../../model/tools')

const fs = require('fs')

router.get('/', async (req, res) => {
  let result = await FocusModel.find({}).sort({_id: -1});
  res.render('admin/focus/index', {
    list: result
  })
})

router.get('/add', (req, res) => {
  res.render('admin/focus/add', {})
  // res.send("增加导航")
})

router.get('/delete', async (req, res) => {
  var _id = req.query.id
  let resultList = await FocusModel.find({_id})

  let delRresult = await FocusModel.deleteOne({_id})
  if(delRresult && delRresult.deletedCount > 0) {
    if(resultList[0].focus_img) {
      fs.unlink('static/' + resultList[0].focus_img, (err) => {})
    }
    res.send({
      code: 200,
      msg: '成功'
    })
  } else {
    res.send({
      code: 404,
      msg: '失败'
    })
  }
})

router.get('/edit', async function (req, res) {
  // 获取管理员数据
  var id = req.query.id
  let result = await FocusModel.find({'_id': id});
  if(result.length > 0) {
    res.render('admin/focus/edit', {
      list: result[0]
    })
  } else {
    res.redirect('/admin/focus')
  }
})

router.post('/doAdd', multer().single('focus_img'), async (req, res) => {

  var focus_img = req.file ? req.file.path.substr(7): '';
  var focusImgObj = {"focus_img": focus_img}

  const focus = new FocusModel(Object.assign(req.body, focusImgObj, {
    create_time: getUnix()
  }))

  let result = await focus.save()
  if(result) {
   res.render('admin/public/success', {
    redirectUrl: '/admin/focus',
    message: '添加成功'
  })
  } else {
    res.render('admin/public/error', {
      redirectUrl: '/admin/focus/add',
      message: '添加失败'
    })
  }
})

router.post('/doEdit', multer().single('focus_img'), async (req, res) => {
  const _id = req.body.id
  const type = req.body.type
  const title = req.body.title
  const link = req.body.link
  const status = req.body.status
  const sort = req.body.sort

  if(req.file) { // 更新了图片
    var focus_img = req.file ? req.file.path.substr(7): '';
    let result = await FocusModel.updateOne({_id},
      Object.assign(
        req.body, {
          focus_img,
          modify_time: getUnix()
        }
      )
    )
    if(result.modifiedCount > 0) {
      res.render('admin/public/success', {
        redirectUrl: '/admin/focus',
        message: '成功'
      })
    } else {
      res.render('admin/public/error', {
        redirectUrl: '/admin/focus/edit?id=' + _id,
        message: '失败'
      })
    }
  } else {
    let focus = await FocusModel.updateOne({_id}, {
      type, title, link, status, sort, modify_time: getUnix()
    })
    if(focus.modifiedCount > 0) {
      res.render('admin/public/success', {
        redirectUrl: '/admin/focus',
        message: '成功'
      })
    } else {
      res.render('admin/public/error', {
        redirectUrl: '/admin/focus/edit?id=' + _id,
        message: '失败'
      })
    }
  }
})

module.exports = router
